import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;

@Getter
@Setter
public class ChatClient {
    Socket socket;
    ObjectOutputStream oos;
    ObjectInputStream ois;
    boolean connected_status = false;
    JTextArea jTextArea;

    public static void main(String[] args) throws IOException {

        ChatClient client = new ChatClient();
        while(!client.isConnected_status()){
            System.out.println("服务器连接中...");
            client.connect();
        }

    }
    public void connect() {
        try {
            socket = new Socket("localhost", 9900);
//            连接成功，跳出循环
            connected_status = true;
            ChatUI chatroomUI = new ChatUI(this, socket);
            jTextArea = chatroomUI.getRecordPane();
        } catch (IOException e) {
//            e.printStackTrace();
            System.out.println("服务器连接失败，正在重连0.0");
        }
    }

    public void exit() {
        Timer timer = new Timer();
        jTextArea.append("服务器出问题了，3秒后退出登录");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.exit(-1);
            }
        },3000);
    }
}